package com.gaogzhen.datastructure.symbolTable;

import edu.princeton.cs.algs4.ST;
import edu.princeton.cs.algs4.StdOut;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Administrator
 * @version 1.0
 * @description
 * @date 2022-11-10 20:57
 */
public class FrequencyCounter {
    public static void main(String[] args) {
        int minLen = Integer.parseInt(args[0]);
        ST<String, Integer> st = new ST<>();
        String filename = args[1];
        try(BufferedReader fr =new BufferedReader(new FileReader(new File(filename)))) {
            String word = null;
            while ((word = fr.readLine()) != null) {
                // 用符号表统计频率
                // 忽略长度小于阈值的单词
                String[] words = word.split("\\s");
                for (String s : words) {
                    if (s.length() < minLen) {
                        continue;
                    }
                    // 单词作为键，如果第一次出现，值设置为1；其他单词对应的值加1
                    if (!st.contains(s)) {
                        st.put(s, 1);
                    } else {
                        st.put(s, st.get(s) + 1);
                    }
                }

            }
            String maxKey = null;
            int max = 0;
            for (String key : st.keys()) {
                Integer count = st.get(key);
                if (count > max) {
                    maxKey = key;
                    max = count;
                }
            }
            StdOut.println(maxKey + " " + max);
        }catch (IOException e) {
            e.printStackTrace();
        }

    }
}
